package com.example.myadmin.student.controller;

import com.example.myadmin.common.vo.Result;
import com.example.myadmin.dept.entity.Dept;
import com.example.myadmin.dept.service.DeptService;
import com.example.myadmin.student.entity.Student;
import com.example.myadmin.student.service.StudentService;
import com.example.myadmin.student.vo.StudentQuery;
import com.example.myadmin.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private DeptService deptService;

    //跳转到学生信息列表
    @GetMapping("")
    public String toStudentListUI(){
        return "student/StuList";
    }

    //学生信息查询
    @ResponseBody
    @GetMapping("/list")
    public Result<Object>  getStudentList(StudentQuery studentQuery){
        List<Student> studentList = studentService.getStudentList(studentQuery);
        //分页查询总记录数
        Long count =studentService.countStudentList(studentQuery);
        return Result.success(studentList,count);
    }

    //提交插入学生的post请求
    @ResponseBody
    @PostMapping("")
    public Result<Object> insertStudent(Student student){
        studentService.insertStudent(student);
        return Result.success("新增成功！");
    }

    //跳转到插入学生的页面
    @GetMapping("/insertStudent")
    public String insertStu(Model model){
        List<Dept> deptList=deptService.getAllDept();
        model.addAttribute("deptList",deptList);
        return "student/insertStudent";
    }
    //删除学生的请求
    //{ids} :带占位符的url，用{}表示
    //配合@PathVariable将占位符中的参数绑定到方法的参数，请求路径的填充就会自动赋值给函数形参ids
    @DeleteMapping("/{ids}")
    @ResponseBody
    public Result<Object> deleteStudentByIds(@PathVariable(name = "ids") String ids){
        studentService.deleteStudentByIds(ids);
        return Result.success("删除学生成功！");
    }

    //根据id查询学生数据
    @GetMapping("/{id}")
    public String  selectStudentById(
            @PathVariable("id") Integer id,
            Model model
    ){
        Student student = studentService.selectStudentById(id);
        model.addAttribute("student",student);
        model.addAttribute("deptList",deptService.getAllDept());
        return "student/updateStudent";
    }

    @PutMapping("")
    @ResponseBody
    public Result<Object> updateStudentById(Student student){
        studentService.updateStudentById(student);
        return Result.success("学生信息修改成功！");
    }
}
